package com.example.carrierapp.ui.person.modifyphone;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.carrierapp.base.BaseActivity;
import com.example.carrierapp.base.MVPBaseActivity;
import com.example.carrierapp.bean.person.ImageCodeBean;
import com.example.carrierapp.config.ConstValue;
import com.example.carrierapp.ui.person.modifypass.ModifyPassContract;
import com.example.carrierapp.ui.person.modifypass.ModifyPassPresenter;
import com.example.carrierapp.ui.view.CreatCode;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.GlideApp;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyPhoneActivity extends MVPBaseActivity<ModifyPhoneContract.View, ModifyPhonePresenter> implements ModifyPhoneContract.View{
    @BindView(R.id.cons_step2)
    ConstraintLayout mConsStep2;
    @BindView(R.id.et_remote_code2)
    EditText mEtRemoteCode2;
    @BindView(R.id.et_local_code2)
    EditText mEtLocalCode2;
    @BindView(R.id.ib_local_code2)
    ImageView mIbLocalCode2;
    @BindView(R.id.tv_login_code2)
    TextView mTvCode2;

    @BindView(R.id.cons_step1)
    ConstraintLayout mConsStep1;
    @BindView(R.id.ib_local_code)
    ImageView mIbLocalCode;
    @BindView(R.id.tv_old_phone)
    TextView mTvOldPhone;
    @BindView(R.id.et_local_code)
    EditText mEtLocalCode;
    @BindView(R.id.et_remote_code)
    EditText mEtRemoteCode;
    @BindView(R.id.tv_login_code)
    TextView mTvCode;

    @BindView(R.id.et_new_phone)
    EditText mEtNewPhone;
    private String mPhone;
    private ImageCodeBean mImageCodeBean;
    private ImageCodeBean mImageCodeBean2;

    @Override
    public int getContentView() {
        return R.layout.activity_modify_phone;
    }

    @Override
    public String getTitleString() {
        return "修改手机号码";
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        mIbBack.setOnClickListener(v -> {
            onBack();
        });
        mPhone = getIntent().getStringExtra(ConstValue.VALUE);
        mPresenter.attachView(this);
        mPresenter.getImgaeCode(true);
        mTvOldPhone.setText("当前号码：" + mPhone);

    }

    private void onBack() {
        if (mConsStep1.getVisibility() == View.VISIBLE)//第一步
        {
            finish();
        } else {
            mConsStep1.setVisibility(View.VISIBLE);
            mConsStep2.setVisibility(View.GONE);
            setTitleString("修改手机号码");
            mVerificationTimer2.cancel();
            mTvCode2.setEnabled(true);
            mTvCode2.setTextColor(Color.parseColor("#2ecc71"));
            mTvCode2.setText(getString(R.string.tv_resend_code));
        }
    }

    @OnClick({R.id.tv_next_step, R.id.tv_suer_modify, R.id.ib_local_code, R.id.tv_login_code2, R.id.tv_login_code, R.id.ib_local_code2})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_next_step:
                if (TextUtils.isEmpty(mEtLocalCode.getText().toString().trim())) {
                    ToastUtils.showCenterToast(this, "请输入图形验证码");
                    return;
                }

                if (TextUtils.isEmpty(mEtRemoteCode.getText().toString().trim())) {
                    ToastUtils.showCenterToast(this, "请输入短信验证码");
                    return;
                }
                showProgress();
                mPresenter.nextStep(mPhone,mEtLocalCode.getText().toString().trim(),mImageCodeBean.guid,mEtRemoteCode.getText().toString().trim());
                break;
            case R.id.tv_suer_modify:
                if(TextUtils.isEmpty(mEtNewPhone.getText().toString().trim()))
                {
                    ToastUtils.showCenterToast(this,"请输入新手机号码");
                    return;
                }
                if(TextUtils.isEmpty(mEtLocalCode2.getText().toString().trim()))
                {
                    ToastUtils.showCenterToast(this,"请输入图形验证码");
                    return;
                }
                if(TextUtils.isEmpty(mEtRemoteCode2.getText().toString().trim()))
                {
                    ToastUtils.showCenterToast(this,"请输入短信验证码");
                    return;
                }
                mPresenter.changePhone(mPhone,mEtNewPhone.getText().toString().trim(),mEtRemoteCode2.getText().toString().trim());
                break;
            case R.id.ib_local_code:
                showProgress();
                mPresenter.getImgaeCode(true);
                break;
            case R.id.ib_local_code2:
                showProgress();
                mPresenter.getImgaeCode(false);
                break;
            case R.id.tv_login_code2:
                if(TextUtils.isEmpty(mEtNewPhone.getText().toString().trim()))
                {
                    ToastUtils.showCenterToast(this,"请输入新手机号码");
                    return;
                }
                if (!TextUtils.isEmpty(mEtLocalCode2.getText().toString().trim())) {
                    if (mImageCodeBean2 != null) {
                        showProgress();
                        mPresenter.getMessageCode(mEtNewPhone.getText().toString().trim(), mEtLocalCode2.getText().toString().trim(), mImageCodeBean2.guid,true);
                    } else {
                        ToastUtils.showCenterToast(this, "请重新获取图形验证码");
                    }
                } else {
                    ToastUtils.showCenterToast(this, "请输入图形验证码");
                }

                break;
            case R.id.tv_login_code:
                if (!TextUtils.isEmpty(mEtLocalCode.getText().toString().trim())) {
                    if (mImageCodeBean != null) {
                        showProgress();
                        mPresenter.getMessageCode(mPhone, mEtLocalCode.getText().toString().trim(), mImageCodeBean.guid,false);
                    } else {
                        ToastUtils.showCenterToast(this, "请重新获取图形验证码");
                    }
                } else {
                    ToastUtils.showCenterToast(this, "请输入图形验证码");
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            onBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected boolean onOverrideBackClick() {
        return true;
    }

    private CountDownTimer mVerificationTimer = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTvCode.setEnabled(false);
            mTvCode.setTextColor(Color.parseColor("#999999"));
            mTvCode.setText(getString(R.string.account_get_verification_code_cd, millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            mTvCode.setEnabled(true);
            mTvCode.setTextColor(Color.parseColor("#2ecc71"));
            mTvCode.setText(getString(R.string.tv_resend_code));
        }
    };

    private CountDownTimer mVerificationTimer2 = new CountDownTimer(60 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTvCode2.setEnabled(false);
            mTvCode2.setTextColor(Color.parseColor("#999999"));
            mTvCode2.setText(getString(R.string.account_get_verification_code_cd, millisUntilFinished / 1000));
        }

        @Override
        public void onFinish() {
            mTvCode2.setEnabled(true);
            mTvCode2.setTextColor(Color.parseColor("#2ecc71"));
            mTvCode2.setText(getString(R.string.tv_resend_code));
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mVerificationTimer.cancel();
    }

    @Override
    protected ModifyPhonePresenter createPresenter() {
        return new ModifyPhonePresenter(this);
    }

    @Override
    public void getImgaeCodeSuccess(boolean firstStep,ImageCodeBean bean) {
        dismissProgress();
        if (bean != null) {
            if(firstStep)
            {
                mImageCodeBean = bean;
                mImageCodeBean.imageUrl = "http://10.130.0.128:10020" + bean.imageUrl;
                GlideApp.with(this).load(bean.imageUrl).into(mIbLocalCode);
            }else {
                mImageCodeBean2 = bean;
                mImageCodeBean2.imageUrl = "http://10.130.0.128:10020" + bean.imageUrl;
                GlideApp.with(this).load(bean.imageUrl).into(mIbLocalCode2);
            }
        }
    }

    @Override
    public void getImgaeCodeField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    public void getMessageCodeSuccess() {
        dismissProgress();
        if (mConsStep1.getVisibility() == View.VISIBLE)//第一步
        {
            mVerificationTimer.start();
        }else
        {
            mVerificationTimer2.start();
        }
    }

    @Override
    public void getMessageCodeField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    public void changePhoneSuccess() {
        dismissProgress();
        finish();
        ToastUtils.showCenterToast(this, "手机号码修改成功");
    }

    @Override
    public void changePhoneField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    public void nextStepSuccess() {
        dismissProgress();

        mVerificationTimer.cancel();
        mTvCode.setEnabled(true);
        mTvCode.setTextColor(Color.parseColor("#2ecc71"));
        mTvCode.setText(getString(R.string.tv_resend_code));
        mPresenter.getImgaeCode(false);

        mConsStep2.setVisibility(View.VISIBLE);
        mConsStep1.setVisibility(View.GONE);
        setTitleString("验证新手机号码");
    }

    @Override
    public void nextStepField(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }
}
